using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web;

namespace BO.Cache
{
    public class PageBase : Page
    {
        private string _cacheName = "";

        public string CacheName
        {
            get { return _cacheName; }
            set { _cacheName = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            string m_CacheContent = "";
            object cacheobject = HttpContext.Current.Cache.Get(_cacheName);
            if (cacheobject != null)
            {
                m_CacheContent = cacheobject.ToString();
            }
            else
            {
                StringBuilder strBuilder = new StringBuilder();
                using (StringWriter strWriter = new StringWriter(strBuilder))
                {
                    using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
                    {
                        base.Render(htmlWriter);
                        m_CacheContent = strBuilder.ToString().Trim();
                        if (Settings.GetBoolean(Settings.Keys.HTML_OUTPUT_ONE_LINE))
                        {
                            m_CacheContent = m_CacheContent.Replace("\t", string.Empty);
                            m_CacheContent = m_CacheContent.Replace("\n", String.Empty);
                            m_CacheContent = m_CacheContent.Replace("    ", string.Empty);
                            m_CacheContent = m_CacheContent.Replace("  ", " ");
                        }
                    }
                }
                HttpContext.Current.Cache.Insert(_cacheName, m_CacheContent, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
            }

            //finish the job
            writer.Write(m_CacheContent);
        }
    }
}
